//
//  EreaListModel.m
//  CarNetwork_logistics
//
//  Created by 高启 on 15/11/12.
//  Copyright © 2015年 turingEdu. All rights reserved.
//

#import "EreaListModel.h"
#import <LKDBHelper.h>
#import <FMDB.h>

@implementation EreaListModel
{
    FMDatabase *db;
}
/**
 *  返回数据库主键
 *
 *  @return 数据库主键
 */
+(NSString *)getPrimaryKey
{
    return @"_id";
}


/**
 *  返回数据库版本号
 *
 *  @return 数据库版本号
 */
+(int)getTableVersion
{
    return 5;
}
/**
 *  返回表名字
 *
 *  @return 数据表名字
 */
+(NSString *)getTableName
{
    return [NSString stringWithFormat:@"%@_version_%d",self.class,[self getTableVersion]];
}


+ (EreaListModel *)sharedManager
{
    static EreaListModel *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}

/**
 *  获取省得列表
 *
 *  @return <#return value description#>
 */
- (NSMutableArray *)getProvinceList
{
   [self openDB];//打开数据库
    if (![db open])
    {
        NSLog(@"数据库打开失败！");
    }
    else
    {
        NSLog(@"打开成功");
    }
    NSMutableArray *temp = [[NSMutableArray alloc]init];

    // 1.执行查询语句
    FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM province_info ORDER BY sortLetters "];

    // 2.遍历结果
    while ([resultSet next])
    {
        
         NSString *name = [resultSet stringForColumn:@"name"];
         NSString *code = [resultSet stringForColumn:@"code"];
         NSString *sortLetters = [resultSet stringForColumn:@"sortLetters"];
       
         NSDictionary *dic  = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",code,@"code",sortLetters,@"sortLetters", nil];
        [temp addObject:dic];
    }
    
    [db close];
    return temp;
    
}
/**
 *  获取市的列表
 *
 *  @param parentid 市所属的省得id
 *
 *  @return <#return value description#>
 */
- (NSMutableArray *)getCityWithProvinceId:(NSString *)provinceId
{
    [self openDB];//打开数据库
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    
    
    // 1.执行查询语句
    NSString *str = [NSString stringWithFormat:@"SELECT name FROM city_info  where provincecode = '%@' ORDER BY sortLetters", provinceId];
    FMResultSet *resultSet = [db executeQuery:str];
    //FMResultSet *resultSet = [db executeQuery:@"SELECT name FROM city_info  where provincecode = '%@' ORDER BY sortLetters",provinceId];
    
         // 2.遍历结果
         while ([resultSet next])
         {
             
             NSString *name = [resultSet stringForColumn:@"name"];
             NSString *code = [resultSet stringForColumn:@"code"];
             NSString *sortLetters = [resultSet stringForColumn:@"sortLetters"];
             NSString *provincecode = [resultSet stringForColumn:@"provincecode"];
             
             NSDictionary *dic  = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",code,@"code",sortLetters,@"sortLetters",provincecode,@"provincecode", nil];
             [temp addObject:dic];
             
             
        }
    [db close];
    
    return temp;
}

- (NSMutableArray *)getContentWithSearchStr:(NSString *)searchStr FromTableName:(NSString *)tabel
{
    [self openDB];//打开数据库
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    NSString *selectStr;
    // 1.执行查询语句
    if ([tabel isEqualToString:@"0"])
    {
        //查询省份
        selectStr = [NSString stringWithFormat:@"SELECT * FROM province_info WHERE name LIKE '%%%@%%'",searchStr];
    }else
    {
        selectStr = [NSString stringWithFormat:@"SELECT * FROM city_info WHERE name LIKE '%%%@%%'",searchStr];
       
    }
    
    FMResultSet *resultSet = [db executeQuery:selectStr];
    
    // 2.遍历结果
    while ([resultSet next])
    {
        
        NSString *name = [resultSet stringForColumn:@"name"];
        NSString *code = [resultSet stringForColumn:@"code"];
        NSString *sortLetters = [resultSet stringForColumn:@"sortLetters"];
        NSString *provincecode = [resultSet stringForColumn:@"provincecode"];
        
        NSDictionary *dic  = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",code,@"code",sortLetters,@"sortLetters",provincecode,@"provincecode", nil];
        [temp addObject:dic];
        
        
    }
    [db close];
    
    return temp;
}

-(NSMutableArray *)getGoodsList
{
    [self openDB];//打开数据库
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    
    // 1.执行查询语句
    FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM transport_medium_info "];
    
    // 2.遍历结果
    while ([resultSet next])
    {
        
        NSString *_id = [resultSet stringForColumn:@"id"];
        NSString *name = [resultSet stringForColumn:@"name"];
    
        NSDictionary *dic  = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",_id,@"_id", nil];
        [temp addObject:dic];
        
        
    }
    [db close];
    
    return temp;
}
/**
 *  打开数据库
 *
 *  @return 返回结果
 */
- (BOOL)openDB
{
    NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString*documentsDirectory =[paths objectAtIndex:0];
    NSString*dbPath =[documentsDirectory stringByAppendingPathComponent:@"erea.db"];
    
    db = [FMDatabase databaseWithPath:dbPath];
    BOOL isOpen = [db open];
    if (!isOpen)
    {
        
        NSLog(@"数据库打开失败！");
        
    }else
    {
        NSLog(@"打开成功");
    }

    return isOpen;
}


@end
